package com.ch.base.dao;

import com.ch.base.model.ChBasicOrderModel;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

/**
 * ch_basic_order dao
 * created in 2019-04-19 10:40
 **/
public class ChBasicOrderModelProvider {


    public String findCountByCondition(ChBasicOrderModel entity) {

        SQL sql = new SQL().SELECT("count(*)").FROM("ch_basic_order");
        condition(sql, entity);
        sql.WHERE("state = '1'");
        return sql.toString();
    }

    public String findOrdersByCondition(ChBasicOrderModel entity) {

        SQL sql = new SQL().SELECT("id,order_id as orderId,order_time as orderTime,receiver_name as receiverName,receiver_phone as receiverPhone," +
                "receiver_address as receiverAddress,province,city,district,item_name as itemName,item_specification as itemSpecification," +
                "purchase_num as purchaseNum,order_money as orderMoney,order_note as orderNote,order_status as orderStatus,state," +
                "created_time as createdTime,updated_time as updatedTime,operator_name as operatorName")
                .FROM("ch_basic_order");
        condition(sql, entity);
        sql.WHERE("state = '1'");
        sql.ORDER_BY(entity.getOrderBy() + " desc");
        return sql.toString() + " limit " + entity.getLimit() + "," + entity.getLimitLen();
    }

    private void condition(SQL sql, ChBasicOrderModel entity) {

        if (StringUtils.isNotEmpty(entity.getOrderId())) {
            sql.WHERE("order_id = #{orderId}");
        }
        if (StringUtils.isNotEmpty(entity.getOrderTime())) {
            sql.WHERE("DATE(order_time) = STR_TO_DATE(#{orderTime}, '%Y-%m-%d')");
        }
        if (StringUtils.isNotEmpty(entity.getReceiverName())) {
            sql.WHERE("receiver_name LIKE CONCAT('%', #{receiverName}, '%')");
        }
        if (StringUtils.isNotEmpty(entity.getReceiverPhone())) {
            sql.WHERE("receiver_phone = #{receiverPhone}");
        }
        if (StringUtils.isNotEmpty(entity.getReceiverAddress())) {
            sql.WHERE("receiver_address LIKE CONCAT('%',#{receiverAddress}, '%')");
        }
        if (StringUtils.isNotEmpty(entity.getProvince())) {
            sql.WHERE("province LIKE CONCAT('%', #{province}, '%')");
        }
        if (StringUtils.isNotEmpty(entity.getCity())) {
            sql.WHERE("city LIKE CONCAT('%', #{city}, '%')");
        }
        if (StringUtils.isNotEmpty(entity.getDistrict())) {
            sql.WHERE("district LIKE CONCAT('%', #{district}, '%')");
        }
        if (StringUtils.isNotEmpty(entity.getItemName())) {
            sql.WHERE("item_name LIKE CONCAT('%', #{itemName}, '%')");
        }
        if (Objects.nonNull(entity.getPurchaseNum())) {
            sql.WHERE("purchase_num = #{purchaseNum}");
        }
        if (Objects.nonNull(entity.getOrderMoney())) {
            sql.WHERE("order_money = #{orderMoney}");
        }
        if (StringUtils.isNotEmpty(entity.getOrderStatus())) {
            sql.WHERE("order_status = #{orderStatus}");
        }
        if (StringUtils.isNotEmpty(entity.getStartTime())) {
            sql.WHERE("STR_TO_DATE(order_time, '%Y-%m-%d') >= STR_TO_DATE(#{startTime}, '%Y-%m-%d')");
        }
        if (StringUtils.isNotEmpty(entity.getEndTime())) {
            sql.WHERE("STR_TO_DATE(order_time, '%Y-%m-%d') <= STR_TO_DATE(#{endTime}, '%Y-%m-%d')");
        }
        if (Objects.nonNull(entity.getRangeDown())) {
            sql.WHERE("order_money >= #{rangeDown}");
        }
        if (Objects.nonNull(entity.getRangeUp())) {
            sql.WHERE("order_money <= #{rangeUp}");
        }
    }
}